package top.lixunda.ecommerce.server.good.api.entity.vo;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.good.api.entity.db.GoodDetailDO;
import top.lixunda.ecommerce.server.good.api.entity.db.GoodPictureDO;
import top.lixunda.ecommerce.server.good.api.entity.db.GoodsDO;
import top.lixunda.ecommerce.server.good.api.entity.db.SubGoodDO;

import java.util.*;

/**
 * @author Xumda
 * @version time:2019/12/19 16:49
 */
@SuppressWarnings("WeakerAccess")
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
public class GoodConvertVO extends GoodsDO {

    /**
     * 图片结合
     */
    private List<GoodPictureDO> goodPictureList;

    /**
     * 商品自定义分类
     */
    private List<SubGoodDO> subGoodList;

    /**
     * 装换为GoodConvertDTO，只有基础属性
     *
     * @param detail GoodDetailDO
     * @return GoodConvertVO
     */
    private static GoodConvertVO convertSimpleData(GoodDetailDO detail) {
        GoodConvertVO convertDTO = new GoodConvertVO();
        convertDTO.setId(detail.getGoodId()).setGroupId(detail.getGroupId()).setGroupName(detail.getGroupName())
                .setStoreId(detail.getStoreId()).setStoreName(detail.getStoreName()).setCreateTime(detail.getCreateGoodTime())
                .setUpdateTime(detail.getUpdateGoodTime()).setRemoveTime(detail.getRemoveGoodTime())
                .setCreateUserId(detail.getCreateGoodUserId()).setCreateUserName(detail.getCreateGoodUserName())
                .setUpdateUserId(detail.getUpdateGoodUserId()).setUpdateUserName(detail.getUpdateGoodUserName())
                .setRemoveUserId(detail.getRemoveGoodUserId()).setRemoveUserName(detail.getRemoveGoodUserName())
                .setName(detail.getGoodName()).setSimpleDesc(detail.getGoodSimpleDesc()).setDescription(detail.getGoodDesc())
                .setGoodCategoryId(detail.getGoodCategoryId()).setGoodCategoryName(detail.getGoodCategoryName())
                .setScore(detail.getGoodScore())
                .setScoreUpdateTime(detail.getGoodScoreUpdateTime()).setSellPoint(detail.getSellPoint())
                .setFreight(detail.getFreight()).setPushTime(detail.getPushGoodTime()).setPullTime(detail.getPullGoodTime())
                .setState(detail.getGoodState()).setStateDesc(detail.getGoodStateDesc()).setCheckTime(detail.getCheckGoodTime())
                .setCheckUserId(detail.getCheckGoodUserId()).setCheckUserName(detail.getCheckGoodUserName())
                .setUseable(detail.getGoodUseable()).setRemark(detail.getGoodRemark());
        return convertDTO;
    }

    /**
     * 装换为GoodPictureDO，只有基础属性
     *
     * @param detail GoodDetailDO
     * @return GoodPictureDO
     */
    public static GoodPictureDO convertGoodPicture(GoodDetailDO detail) {
        if (detail.getPictureId() == null || detail.getImage() == null) {
            return null;
        }
        return new GoodPictureDO().setId(detail.getPictureId()).setGoodId(detail.getGoodId()).setGoodName(detail.getGoodName())
                .setImage(detail.getImage()).setImageFileId(detail.getImageFileId())
                .setRemark(detail.getPictureRemark()).setUseable(detail.getPictureUseable())
                .setOrder(detail.getPictureOrder());
    }

    /**
     * 装换为SubGoodDO，只有基础属性
     *
     * @param detail GoodDetailDO
     * @return SubGoodDO
     */
    public static SubGoodDO convertSubGood(GoodDetailDO detail) {
        if (ObjectValidators.isEmpty(detail) || ObjectValidators.isEmpty(detail.getSubGoodId())) {
            return null;
        }
        return new SubGoodDO().setId(detail.getSubGoodId()).setGoodId(detail.getGoodId()).setGoodName(detail.getGoodName())
                .setSubGoodName(detail.getSubGoodName()).setDescription(detail.getSubDesc()).setImage(detail.getSubImage())
                .setImageFile(detail.getSubImageFile()).setCount(detail.getSubCount()).setPrice(detail.getSubPrice())
                .setOldPrice(detail.getSubOldPrice()).setOldPriceTime(detail.getSubUpdatePriceTime())
                .setCreateTime(detail.getSubCreateTime()).setCreateUserId(detail.getSubCreateUserId())
                .setCreateUserName(detail.getSubCreateUserName()).setUpdateTime(detail.getSubUpdateTime())
                .setUpdateUserId(detail.getSubUpdateUserId()).setUpdateUserName(detail.getSubUpdateUserName())
                .setRemoveTime(detail.getSubRemoveTime()).setRemoveUserId(detail.getSubRemoveUserId())
                .setRemoveUserName(detail.getSubRemoveUserName()).setState(detail.getSubState())
                .setStateDesc(detail.getSubStateDesc()).setCheckTime(detail.getSubCheckTime())
                .setCheckUserId(detail.getSubCheckUserId()).setCheckUserName(detail.getSubCheckUserName())
                .setUseable(detail.getSubUseable()).setRemark(detail.getSubRemark());
    }

    /**
     * 从GoodDetailDO列表中进行级联相似的转换
     *
     * @param detailList List<GoodDetailDO>
     * @return GoodConvertDTO列表
     */
    public static List<GoodConvertVO> fromGoodDetail(List<GoodDetailDO> detailList) {
        if (detailList == null) {
            return null;
        }
        if (detailList.size() == 0) {
            return Collections.emptyList();
        }
        // 保证顺序
        Set<Integer> dtoSet = new LinkedHashSet<>(10);
        Map<Integer, GoodConvertVO> goodCache = new HashMap<>(detailList.size());
        // 防重复
        Map<Integer, Integer> picture = new HashMap<>(detailList.size());
        Map<Integer, Integer> subGood = new HashMap<>(detailList.size());
        GoodConvertVO temp;
        for (GoodDetailDO detail : detailList) {
            temp = goodCache.get(detail.getGoodId());
            if (temp == null) {
                temp = convertSimpleData(detail);
                temp.setGoodPictureList(new ArrayList<>(8));
                temp.setSubGoodList(new ArrayList<>(8));
                dtoSet.add(temp.getId());
                goodCache.put(temp.getId(), temp);
            }
            // 图片
            if (picture.get(detail.getPictureId()) == null) {
                GoodPictureDO goodPictureDO = convertGoodPicture(detail);
                if (ObjectValidators.isNotEmpty(goodPictureDO)) {
                    temp.getGoodPictureList().add(goodPictureDO);
                }
                picture.put(detail.getPictureId(), 1);
            }
            // 子类商品
            if (subGood.get(detail.getSubGoodId()) == null) {
                SubGoodDO subGoodDO = convertSubGood(detail);
                if (ObjectValidators.isNotEmpty(subGoodDO)) {
                    temp.getSubGoodList().add(subGoodDO);
                }
                subGood.put(detail.getSubGoodId(), 1);
            }
        }
        List<GoodConvertVO> list = new ArrayList<>(dtoSet.size());
        for (Integer orderId : dtoSet) {
            list.add(goodCache.get(orderId));
        }
        return list;
    }

}
